package me.nereo.multi_image_selector;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.ListPopupWindow;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.ali.music.multi_image_selector.R;

import java.util.List;

import me.nereo.multi_image_selector.adapter.FolderAdapter;
import me.nereo.multi_image_selector.bean.Folder;
/**
 * Created by yuanxiaowei on 15/10/21.
 */
public class SelectorPhotoFolder implements AdapterView.OnItemClickListener {
    private ListPopupWindow mFolderPopupWindow;
    private FolderAdapter mFolderAdapter;
    private int mHeight = 500;
    private int mWidth = 500;
    private AdapterView.OnItemClickListener mOnItemClickListener;

    /**
     *
     * @param context Context
     */
    public SelectorPhotoFolder(Context context) {
        DisplayMetrics sDisplayMetrics = context.getResources().getDisplayMetrics();
        mWidth = sDisplayMetrics.widthPixels;

        mFolderPopupWindow = new ListPopupWindow(context);
        mFolderPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mFolderPopupWindow.setModal(true);
        setSize(mWidth, mHeight);
        mFolderAdapter = new FolderAdapter(context);
        mFolderPopupWindow.setAdapter(mFolderAdapter);
        mFolderPopupWindow.setOnItemClickListener(this);

//        ListView listView = mFolderPopupWindow.getListView();
//        listView.setDivider(new ColorDrawable(context.getResources().getColor((R.color.folder_divider))));
//        listView.setDividerHeight(1);
    }

    /**
     *
     * @param folders List<Folder>
     */
    public void update(List<Folder> folders) {
        mFolderAdapter.setData(folders);
    }

    /**
     *
     * @param listener OnItemClickListener
     */
    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /**
     *
     * @param listener OnDismissListener
     */
    public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
        mFolderPopupWindow.setOnDismissListener(listener);
    }

    /**
     *
     * @param width width
     * @param height height
     */
    public void setSize(int width, int height) {
        this.mWidth = width;
        this.mHeight = height;
        mFolderPopupWindow.setContentWidth(width);
        mFolderPopupWindow.setWidth(width);
        mFolderPopupWindow.setHeight(height);
    }

    /**
     *
     * @param view View
     */
    public void show(View view) {
        mFolderPopupWindow.setAnchorView(view);
        mFolderPopupWindow.show();
        int index = mFolderAdapter.getSelectIndex();
        index = index == 0 ? index : index - 1;
        mFolderPopupWindow.getListView().setSelection(index);
        ListView listView = mFolderPopupWindow.getListView();
        listView.setDivider(new ColorDrawable(view.getResources().getColor((R.color.multi_folder_divider))));
        listView.setDividerHeight(1);
    }

    /**
     *
     * @return showing return true
     */
    public boolean isShowing() {
        return mFolderPopupWindow.isShowing();
    }

    /**
     * hide self
     */
    public void hide() {
        if (mFolderPopupWindow.isShowing()) {
            mFolderPopupWindow.dismiss();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mFolderAdapter.setSelectIndex(position);
        hide();
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(parent, view, position, id);
        }
    }
}
